@page "/movies/details"
@using Microsoft.EntityFrameworkCore
@using BlazorWebAppMovies.Models
@inject IDbContextFactory<BlazorWebAppMovies.Data.BlazorWebAppMoviesContext> DbFactory
@inject NavigationManager NavigationManager

<PageTitle>Details</PageTitle>

<h1>Details</h1>

<div>
    <h2>Movie</h2>
    <hr />
    @if (movie is null)
    {
        <p><em>Loading...</em></p>
    }
    else {
        <dl class="row">
            <dt class="col-sm-2">Title</dt>
            <dd class="col-sm-10">@movie.Title</dd>
            <dt class="col-sm-2">Release Date</dt>
            <dd class="col-sm-10">@movie.ReleaseDate</dd>
            <dt class="col-sm-2">Genre</dt>
            <dd class="col-sm-10">@movie.Genre</dd>
            <dt class="col-sm-2">Price</dt>
            <dd class="col-sm-10">@movie.Price</dd>
            <dt class="col-sm-2">Rating</dt>
            <dd class="col-sm-10">@movie.Rating</dd>
        </dl>
        <div>
            <a href="@($"/movies/edit?id={movie.Id}")">Edit</a> |
            <a href="@($"/movies")">Back to List</a>
        </div>
    }
</div>

@code {
    private Movie? movie;

    [SupplyParameterFromQuery]
    private int Id { get; set; }

    protected override async Task OnInitializedAsync()
    {
        using var context = DbFactory.CreateDbContext();
        movie = await context.Movie.FirstOrDefaultAsync(m => m.Id == Id);

        if (movie is null)
        {
            NavigationManager.NavigateTo("notfound");
        }
    }
}
